<?php
/**
 * Created by PhpStorm.
 * User: xpz
 * Date: 2023/7/31
 * Time: 16:52
 */

namespace app\api\command\spider;
use app\api\command\spider\SpiderBase;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use think\config;
use think\Db;
use QL\QueryList;

class Bhw extends  SpiderBase
{
    private $baseDomain;
    protected function configure()
    {
        $this->setName('bhw')              		//定义命令的名字
        ->setDescription('bhw')      //定义命令的描述
        ->addArgument('method');                   //参数1
    }

    //    php think bhw spider
    protected function execute(Input $input, Output $output)
    {
        $this->baseDomain = "https://www.baihuawen.cn";
        require EXTEND_PATH.'phpQuery/phpQuery.php';
        ini_set('max_execution_time', 600);//秒为单位，自己根据需要定义
        $method = $input->getArgument('method');
        if($method == 'test'){
            ini_set("memory_limit","500M");
            $url = 'https://www.baihuawen.cn/fanwen/zhufu/207596.html';
            $this->test($url);
        }

    }



    private function test($url){
        echo $url."抓96919取开始+++++++++++++++++++++++\r\n";
        if($this->_checkSpider($url)){
            //获取type类型开始
            $urlArr = parse_url($url);
            $pathArr = explode('/',$urlArr['path']);
            array_shift($pathArr);
            array_pop($pathArr);
            $pathString = '';
            if(!empty($pathArr)){
                $pathString = implode(',',$pathArr);
            }
            //获取type类型结束
            $html = _get_url($url);
            //获取大标题
            $bigTitleString = QueryList::html($html)->find('h1')->htmls();
            $bigTitleString = $bigTitleString[0];
//            print_r($bigTitleString);exit;
            list($bigTitle,$bigTitleDel) = $this->getBigTitle($bigTitleString);
//            print_r($bigTitle);
//            var_dump($bigTitleDel);
//            exit;
            //获取内容开始
            $content = QueryList::html($html)->find('.content')->html();
            if(strpos($content,'</h2>') !== false){
                $content = str_replace(['<pstyle>','</pstyle>'],['',''],$content);
                $content = str_replace(['<h2>','</h2>'],['<p>#','#</p>'],$content);
//                echo $content;exit;
                $contentArr = QueryList::html($content)->find('p')->htmls();
//                print_r($contentArr);
                $return = $xiangguanArr = [];
                $desc = '';
                if(!empty($contentArr)){
//                    print_r($contentArr);
                    foreach($contentArr as $k=>$v){
                        //处理前言
                        if($k == 0){
                            if(strpos($v,'<a') == false){
                                $desc = $v;
                            }
                        }
                        //处理结尾的最新范文
                        if($v == '最新范文'){
                            continue;
                        }
                        //处理相关链接
                        if(strpos($v,'<a') !== false){
                            $xiangguanArr[] = QueryList::html($v)->find('a')->attr('href');
                            continue;
                        }
                        //处理标题做key
//                        echo $v."\r\n";
                        $firest = mb_substr($v,0,1);
//                        var_dump($firest);
                        $end = mb_substr($v,-1,1);
//                        echo $firest;exit;
                        if($firest == '#' && $end == '#'){
//                            echo "2222";
                            $key = $v;
                        }
                        if(!isset($key)){
//                            echo "1";
                            continue;
                        }
//                        var_dump($key);exit;
//                        var_($v);
                        $return[$key][] = $v;
                    }
                    if(!empty($desc)){
                        $searchArr = ['白话文'];
                        $replaceArr = ['web_name'];

                        if(!empty($bigTitleDel)){
                            $searchArr[] = $bigTitleDel;
                            $replaceArr[] = 'replace_title_del';
                        }
                        $desc = str_replace($searchArr,$replaceArr,$desc);
                    }
//                    print_r($desc);exit;
                    if(!empty($return)){
                        foreach($return as $k=>$v){
                            $title = array_shift($v);
                            $content = '';
                            if(!empty($v)){
                                foreach($v as $k=>$v){
                                    $content .="<p>".$this->myTrim($v)."</p>";
                                }
                            }
                            if(empty($content)){
                                continue;
                            }

                            $title = $this->getTitle($title);


                            $data = [
                                'title'=>$title,
                                'content' => $content,
                                'source_url' => $url,
                                'url_md5' => md5($url),
                                'type' => $pathString,
                                'weigh' => rand(0,99999999),
                                'big_title' => $bigTitle,
                                'is_single' => 2,
                                'summary' => $desc,
                            ];
//                            print_r($data);exit;
                            $this->insert($data);
                        }

                    }
                    echo $url."抓取结束==============================\r\n";
                    //抓取相关文章
                    if(!empty($xiangguanArr)){
                        foreach($xiangguanArr as $k=>$v){
                            if(!empty($v)){
                                if(mb_substr($v,-4) == 'html'){
                                    echo $v."\r\n";
                                    $articleModel = Model('app\common\model\BhwTemp');
                                    $tempData = $articleModel->where(['url_md5'=>md5($v)])->find();
                                    if(empty($tempData)){
                                        $articleModel->insertGetId(['source_url'=>$v,'url_md5'=>md5($v)]);
                                    }
                                   $this->test($v);
                                }

                            }

                        }
                    }

//                    print_r($return);exit;
                }
            } else {

            }
        } else {
            echo $url."test已经抓取过\r\n";
        }
    }


    private function getTitle($title){
        $title = myTrim(strip_tags(str_replace('#',"",$title)));
        $searchStr = mb_substr($title,-2,1);
        if($searchStr == '篇'){
            return mb_substr($title,0,mb_strlen($title)-2);
        }
        return $title;
    }

    private function insert($data){
//        print_r($data);
        $articleModel = Model('app\common\model\BhwSpider');
        $content = $data['content'];
        unset($data['content']);
        $id = $articleModel->insertGetId($data);
        $contentArr = [
            'aid'=>$id,
            'content'=>$content,
        ];
        Model('app\common\model\BhwSpiderContent')->insertGetId($contentArr);
    }

    private function _checkSpider($url){
        $articleModel = Model('app\common\model\BhwSpider');
        // var_dump($articleModel);exit;
        $articleData = $articleModel->where(['url_md5'=>md5($url)])->find();
        if(empty($articleData)){
            return true;
        } else {
            return false;
        }
        // var_dump($articleData);
    }
    private function getBigTitle($string){
        $return = [];
        $biaoshi = ['【','（'];
        foreach($biaoshi as $k=>$v){
            $bigTitleArr = explode($v,$string);
//            print_r($bigTitleArr);;
            if(count($bigTitleArr) == 2){
                $return = [$bigTitleArr[0],$v.$bigTitleArr[1]];
                break;
            }
        }

        if(empty($return)){
            $return = $this->delStringEndArrString($string);
        }

        if(empty($return)){
            $return = [$string,''];
        }
        return $return;
    }

    private function delStringEndNum($str){
//    $str = '123abc456';
        $oLen = $len = strlen($str) - 1;
        while (is_numeric(substr($str, $len, 1))) {

            $len--;
        }
        $end = '';
        if($oLen != $len){
            $c = $oLen - $len;
            $end = mb_substr($str,-$c);
        }
        $str = substr($str, 0, $len + 1);
        return [$str,$end];
//    $start = 0;
//    while (is_numeric(substr($str, $start, 1))) {
//        $start++;
//    }
//    $str = substr($str, $start);
    }


    private function delStringEndArrString($str){
        $oLen = $len = mb_strlen($str) - 1;
//    echo $len;
        $arr = ['篇'];
//    echo mb_substr($str, $len, 1)."\r\n";
//    exit;
        while (in_array(mb_substr($str, $len, 1),$arr)) {

            $len--;
        }
        $end = '';
        if($oLen != $len){
            $end = mb_substr($str,-1);
        }
        $str = mb_substr($str, 0, $len + 1);
        //数字
        list($strN,$endN) = $this->delStringEndNum($str);
        if(!empty($end)){
            $end = $endN.$end;
            $str = $strN;
            //查找数字前面的两个字
            $replaceArr = [
                '精选',
                '优秀',
                '最新',
                '精彩',
                '通用',
                '经典',
                '范文',
            ];
            $strSub = mb_substr($str,-2);
            if(in_array($strSub,$replaceArr)){
                $str = mb_substr($str,0,mb_strlen($str)-2);
                $end = $strSub.$end;
            }
//            echo $strSub;exit;
        }

        return [$str,$end];

    }
}